package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: qingtao
 * @Date 2022/3/8 9:57
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    /**
     * 登录
     *
     * @param employee 前端提交的数据
     * @return 返回给用户的信息
     */
    @Override
    public R<Employee> login(Employee employee) {
        //通过提交数据查询数据库数据
        Employee loginEmployee = employeeMapper.login(employee);

        //判断数据库是否有相似的数据
        if (loginEmployee == null) {
            throw new BusinessException("用户名或者密码错误");
        }

        //判断密码是否正确
        String md5 = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if (!loginEmployee.getPassword().equals(md5)) {
            throw new BusinessException("用户名或者密码错误");
        }

        //判断账户状态
        if (loginEmployee.getStatus() == 0) {
            throw new BusinessException("账号是禁用状态，请联系管理员解决");
        }

        return R.success(loginEmployee);
    }

    /**
     * 添加
     *
     * @param employee
     * @return boolean
     */
    @Override
    public boolean save(Employee employee) {
        Employee dbE = employeeMapper.login(employee);
        if (dbE != null) {
            throw new BusinessException("用户名重复");
        }


        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setStatus(1);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        return employeeMapper.save(employee) > 0;
    }

    /**
     * 分页查找
     *
     * @param page     第几页
     * @param pageSize 页面大小
     * @param name     条件
     * @return
     */
    @Override
    public Page<Employee> findByPage(Integer page, Integer pageSize, String name) {

        //设置分页大小
        PageHelper.startPage(page, pageSize);
        //获得数据库的数据列表
        List<Employee> employeeList = employeeMapper.findByPage(name);
        //创建分页
        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);

        //返回实体类Page<> 参数是数据、条数、页面大小、第几页
        return new Page<>(
                pageInfo.getList(), pageInfo.getTotal(),
                pageInfo.getPageSize(), pageInfo.getPageNum()
        );
    }

    /**
     * 反查详情接口
     * @param id
     * @return
     */
    @Override
    public Employee queryEmployeeById(Integer id) {
        return employeeMapper.queryEmployeeById(id);
    }

    /**
     * 修改
     *
     * @param employee
     * @return
     */
    @Override
    public boolean update(Employee employee) {
        Employee loginEmployee = employeeMapper.login(employee);
        if (loginEmployee != null && !loginEmployee.getId().equals(employee.getId())){
            throw new BusinessException("要修改的账户已经存在，请输入一个不存在的账户");
        }
        employee.setUpdateTime(LocalDateTime.now());
        return employeeMapper.update(employee)>0;
    }
}
